package com.api.core.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("ALL")
@Slf4j
public class HttpUtil {

    private static volatile HttpUtil httpUtil;

    private DefaultHttpClient httpClient;

    public HttpUtil(DefaultHttpClient httpClient) {
        this.httpClient = httpClient;
    }

    public static HttpUtil getInstance() {
        try {
            if (null == httpUtil) {
                Thread.sleep(1000);
                synchronized (HttpUtil.class) {
                    if (null == httpUtil) {
                        DefaultHttpClient myhttpClient = new DefaultHttpClient();
                        httpUtil = new HttpUtil(myhttpClient);
                    }
                }
            }
        } catch (InterruptedException e) {
        }
        return httpUtil;
    }

    public String httpGet(String url) {
        String content = "";
        HttpEntity entity = null;
        HttpGet httpget = null;
        httpget = new HttpGet(url);
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpget);
            int repStatusCode = httpResponse.getStatusLine().getStatusCode();
            if (repStatusCode != 200) {
                System.out.println("HTTP返回码:" + repStatusCode);
            }
            entity = httpResponse.getEntity();
            content = EntityUtils.toString(entity, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            httpget.abort();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return content;

    }


    public String httpPost(String url, String message) {
        String content = "";
        HttpEntity entity = null;
        HttpPost httpPost = null;
        httpPost = new HttpPost(url);
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpPost);
            int repStatusCode = httpResponse.getStatusLine().getStatusCode();
            if (repStatusCode != 200) {
                System.out.println("HTTP返回码:" + repStatusCode);
            }
            entity = httpResponse.getEntity();
            System.out.println("应答码:{}" + httpResponse.getStatusLine().getStatusCode());
            content = EntityUtils.toString(entity, "utf-8");
            System.out.println("应答报文:{}" + content);

        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            httpPost.abort();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return content;

    }


    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            //打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            //设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        } finally {
            //使用finally块来关闭输出流、输入流
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }


    public String httpPostToken(String url,String message,String token){
        CloseableHttpResponse response;
        String rsStr ="";
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("data", message));
        try {
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "utf-8");
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("Authorization", token);
            httppost.setEntity(entity);
            response = httpClient.execute(httppost);
            HttpEntity rentity = response.getEntity();
            if (rentity != null) {
                rsStr = EntityUtils.toString(rentity, "utf-8");
                //System.out.println("返回数据:"+rsStr);
            }
            httppost.abort();
        } catch (IOException e) {

        }

        return  rsStr;

    }

    public String wxPayPost(String url,String message){
        System.out.println("请求报文="+message);
        CloseableHttpResponse response;
        String rsStr ="";
        try {
            HttpPost httppost = new HttpPost(url);
            if(null != message) {
                StringEntity stringEntity = new StringEntity(message.toString(), "UTF-8");
                httppost.setEntity(stringEntity);
            }
            response = httpClient.execute(httppost);
            HttpEntity rentity = response.getEntity();
            if (rentity != null) {
                rsStr = EntityUtils.toString(rentity, "utf-8");
                //System.out.println("返回数据:"+rsStr);
            }
            httppost.abort();
        } catch (IOException e) {

        }

        return  rsStr;

    }




}
